﻿using CloudMusic.Helper;
using CloudMusic.Model;
using CloudMusic.ViewModel;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CloudMusic.UC
{
    /// <summary>
    /// UCUserInfo.xaml 的交互逻辑
    /// </summary>
    public partial class UCUserInfo : UserControl
    {
        List<userInfo> us = new List<userInfo>();
        DataServiceHelper ds = new DataServiceHelper();
        public UCUserInfo()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 失去焦点托管
        /// </summary>
        public event RoutedEventHandler LostFocusHide;
        public event RoutedEventHandler loginOut;

        public static event EventHandler recLoginOut;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("1");
        }
        /// <summary>
        /// 失去焦点时后
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void userInfo_LostFocus(object sender, RoutedEventArgs e)
        {
            var focused_element = FocusManager.GetFocusedElement(Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive));
            var parent = (focused_element as FrameworkElement).TryFindParent<UCUserInfo>();
            if (parent == null) LostFocusHide?.Invoke(this, e);
        }
        /// <summary>
        /// 用户签到
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void signinBtn_Click(object sender, RoutedEventArgs e)
        {
            var flag = CommonHelper.Get("http://localhost:3000/daily_signin?cookie=" + PlayListHelper.UserInfos[0].Cookie);
            JObject job = (JObject)JsonConvert.DeserializeObject(flag);
            if (job["code"].ToString() == "200")
            {
                signinBtn.IsEnabled = false;
                signinIcon.Visibility = Visibility.Collapsed;
                signinName.Text = "已签到";
                PlayListHelper.UserInfos[0].SiginIn = 1;
            }
            else if (job["code"].ToString() == "-5")
            {
                MessageBox.Show(job["msg"].ToString());
            }
        }
        /// <summary>
        /// 鼠标上移触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void signinBtn_MouseEnter(object sender, MouseEventArgs e)
        {
            signinBtn.Opacity = 1;
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Loginout_Click(object sender, RoutedEventArgs e)
        {
           
            //UpdataLoginState
            var strJosn = CommonHelper.Get("http://localhost:3000/logout?cooike="+ PlayListHelper.UserInfos[0].Cookie);
            JObject obj = (JObject)JsonConvert.DeserializeObject(strJosn);
            if (obj["code"].ToString() == "200")
            {
                loginOut?.Invoke(null, null);
                recLoginOut?.Invoke(null, null);
                userImg.ImageSource = new BitmapImage(new Uri(@"pack://application:,,,/Image/demo.jpg"));
                userName.Text = "登陆后查看";
                dt.Content = 0;
                fs.Content = 0;
                gz.Content = 0;
                vipType.Visibility = Visibility.Collapsed;
                userName.Width = 105;
                PlayListHelper.UserInfos[0].LonginState = 0;
                PlayListHelper.UserInfos.OrderBy(t => t.UserId).ToList().ForEach(t => us.Add(t));
                ds.UpdataLoginState(us);
                //ucuserinfo.Visibility = Visibility.Collapsed;
            }
        }
    }
}
